package database

import (
	"Salary/model"
)

type PayrollDatabase struct {
	employees    map[int]model.Employee
	unionMembers map[int]int
}

func (db *PayrollDatabase) clear() {
	db.employees = make(map[int]model.Employee)
	db.unionMembers = make(map[int]int)
}

func (db *PayrollDatabase) GetEmployee(employee_id int) *model.Employee {
	employee, ok := db.employees[employee_id]
	if ok {
		return &employee
	} else {
		return nil
	}
}

func (db *PayrollDatabase) AddEmployee(employee_id int, e model.Employee) {
	db.employees[employee_id] = e
}

func (db *PayrollDatabase) RemoveEmployee(employee_id int) {
	delete(db.employees, employee_id)
}

func (db *PayrollDatabase) AddUnionMember(member_id int, e model.Employee) {
	db.unionMembers[member_id] = e.EmployeeId
}

func (db *PayrollDatabase) GetUnionMember(member_id int) *model.Employee {
	employee_id, ok := db.unionMembers[member_id]
	if ok {
		return db.GetEmployee(employee_id)
	} else {
		return nil
	}
}

func (db *PayrollDatabase) RemoveUnionMember(member_id int) {
	delete(db.unionMembers, member_id)
}

func (db *PayrollDatabase) GetAllEmployees() []model.Employee {
	var ret []model.Employee = make([]model.Employee, 0)
	for _, e := range db.employees {
		ret = append(ret, e)
	}
	return ret
}

var gPayrollDatabaseInstance *PayrollDatabase

func GetPayrollDatabaseInstance() *PayrollDatabase {
	if gPayrollDatabaseInstance == nil {
		gPayrollDatabaseInstance = &PayrollDatabase{
			make(map[int]model.Employee, 0),
			make(map[int]int, 0),
		}
	}
	return gPayrollDatabaseInstance
}
